Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){
	var showType = 1;
	var categoryTree = new Ext.tree.TreePanel({
		title:'用户类别',
		region:'west',
		frame:true,
		width:200,
		split:true,
		collapsible:true,
		rootVisible : false,
		autoScroll : true,
		root:new Ext.tree.AsyncTreeNode({
			id : 'root',
			text : '类别'
		}),
		loader: new Ext.tree.TreeLoader({
			url:'/wsd/admin/data/user_command_tree.json'
    	})
	});
	categoryTree.expandAll();

	var sm = new Ext.grid.CheckboxSelectionModel();
	
	var cm = new Ext.grid.ColumnModel([
		sm,
		{header:'会员编号',dataIndex:'userid'},
		{header:'会员名',dataIndex:'username'},
		{header:'会员姓名',dataIndex:'UName'},
		{header:'会员级别',dataIndex:'userLevel',renderer:function(value){
			switch(value){
				case 1:
					value = '普通会员';
					break;
				case 2:
					value = '高级会员';
					break;
				case 3:
					value = 'VIP会员';
					break;
			}
			return value;
			
		}},
		{header:'手机',dataIndex:'mobile'},
		{header:'公司',dataIndex:'company'},
		{header:'状态',dataIndex:'state',renderer:function(value){
			var result = '已激活';
			if(value == false){
				result = '未激活';
			}
			return result;
		}}
	])
	
	var userStore = new Ext.data.JsonStore({
		url:'/wsd/admin/user_search.action',
		totalProperty:'total',
		root:'result.users',
		autoLoad:true,
		baseParams:{
			'user.userLevel':0,
			showType:1,
			key:'',
			start:0,
			limit:10
		},
		fields:['userid','username','UName','userLevel','mobile','phone','company','state','address','comment','email','state']
	})
	var stateCombo = new Ext.form.ComboBox({
		fieldLabel:'会员状态',
		mode:'local',
		name:'text',
		triggerAction:'all',
		valueField:'user.state',
		displayField:'state',
		value:1,
		editable:false,
		store:new Ext.data.SimpleStore({
			fields:['user.state','state'],
			data:[
				['1','所有状态'],
				['2','已激活'],
				['3','未激活']
			]
		})
	})
	stateCombo.on('select',function(combo,record,i){
		var showType = stateCombo.getValue();//取得当前状态
		userStore.load({
			params:{
				showType:showType
			
			}
		})
		
	});
	
	var infoForm = new Ext.form.FormPanel({
		region:'north',
		height:40,
		frame:true,
		items:[stateCombo]
	})
	var userPanel = new Ext.grid.GridPanel({
		region:'center',
		store:userStore,
		viewConfig:{
			forceFit:true
		},
		loadMask:true,
		autoScroll:true,
		cm:cm,
		sm:sm,
		bbar:new Ext.PagingToolbar({
			pageSize:10,
			store:userStore,
			displayInfo:true,
			displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			emptyMsg:'没有记录'
		}),
		tbar:new Ext.Toolbar([
			{
				text:'查看详细',
				handler:function(){
					var record = userPanel.getSelectionModel().getSelected();
					if(record == null){
						Ext.Msg.alert('系统信息','请先选择会员');
						return false;
					}
					var user = record.data;
					createModifyWindow(user);
				
				}
			},
			'->',
			{
				text:'激活',
				handler:function(){
					var records = sm.getSelections();
					updateState(records,true);
					
				}
			},
			'|',
			{
				text:'冻结',
				handler:function(){
					var records = sm.getSelections();
					updateState(records,false);
				}
			
			},'|',
			{
				text:'修改级别',
				handler:function(){
					var records = sm.getSelections();
					createUpdateLevelWindow(records);
				}
			}
		])
	})
	//中间主面板
	var panel = new Ext.Panel({
		region:'center',
		layout:'border',
		items:[infoForm,userPanel]
	})
	
	
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [categoryTree,panel]
	});
	categoryTree.on("click",function(node,e){
		stateCombo.setValue('1');
		userStore.baseParams['showType'] = 1;//默认为全部状态
		userStore.baseParams['user.userLevel'] = node.attributes.id;
		userStore.load({
			params:{
				start:0,
				limit:10
			}
		})
	})
	/**
	 * 创建修改查看窗口
	 */
	function createModifyWindow(user){
		var userIdField = new Ext.form.Hidden({
			name:'user.userid',
			value:user.userid
		})
		var userNameField = new Ext.form.TextField({
			fieldLabel:'用户名(*)',
			name:'user.username',
			blankText:'用户名不能为空',
			msgTarget:'under',
			allowBlank:false,
			value:user.username,
			disabled:true
		})
		
		var nameField = new Ext.form.TextField({
			fieldLabel:'姓名(*)',
			name:'user.UName',
			blankText:'用户姓名不能为空',
			msgTarget:'under',
			allowBlank:false,
			value:user.UName
		})
		var emailField = new Ext.form.TextField({
			fieldLabel:'用户邮箱',
			name:'user.email',
			width:200,
			value:user.email
		})
		var mobileField = new Ext.form.NumberField({
			fieldLabel:'手机(*)',
			name:'user.mobile',
			blankText:'用户手机不能为空',
			msgTarget:'under',
			allowBlank:false,
			value:user.mobile
		})
		var phoneField = new Ext.form.NumberField({
			fieldLabel:'座机',
			name:'user.phone',
			msgTarget:'under',
			value:user.phone
		})
		var userTypeCombo = new Ext.form.ComboBox({
			fieldLabel:'会员类型',
			mode:'local',
			name:'text',
			triggerAction:'all',
			valueField:'user.userLevel',
			displayField:'userLevel',
			value:user.userLevel,
			editable:false,
			store:new Ext.data.SimpleStore({
				fields:['user.userLevel','userLevel'],
				data:[
					['1','普通会员'],
					['2','高级会员'],
					['3','VIP会员']
				]
			})
		})
		var addressField = new Ext.form.TextField({
			fieldLabel:'地址(*)',
			name:'user.address',
			blankText:'地址不能为空',
			msgTarget:'under',
			allowBlank:false,
			width:300,
			value:user.address
		})
		var companyField = new Ext.form.TextField({
			fieldLabel:'公司(*)',
			allowBlank:false,
			blankText:'公司名不能为空',
			msgTarget:'under',
			name:'user.company',
			value:user.company
		})
		var commentField = new Ext.form.TextArea({
			fieldLabel:'备注',
			name:'user.comment',
			width:300,
			height:100,
			value:user.comment
		})
		var modifyBtn = new Ext.Button({
			text:'修改',
			handler:function(){
				if(!form.getForm().isValid()){
					return;
				}
				form.getForm().submit({
					url:'admin/user_updateUser.action',
					params:{
						'user.username':userNameField.getValue(),
						'user.userLevel':userTypeCombo.getValue()
					},
					waitMsg: '正在修改，请稍后……',
					success: function(fp, o){
						userStore.reload();
			       		Ext.Msg.alert('系统信息',"修改成功!");
		            },
		            failure:function(fp, o){
		            	Ext.Msg.alert('系统信息',o.result.result.msg);
		            }
				})
			}
		})
		var exitBtn = new Ext.Button({
			text:'退出',
			handler:function(){
				userWin.close();
			}
		
		})
		var form = new Ext.form.FormPanel({
			region:'center',
			frame:true,
			items:[userIdField,userNameField,nameField,emailField,mobileField,phoneField,
					userTypeCombo,addressField,companyField,commentField],
			buttons:[modifyBtn,exitBtn]
		})
		var userWin = new Ext.Window({
			title:'会员' + user.username + ' 的信息',
			layout:'border',
			width:650,
			height:400,
			maximizable:true,
			closeAction:'hide',
			modal:true,
			constrain:true,//防止窗口超出边界,
			items:[form]
		})
	
		userWin.show();
	}


	function search(){
		var key = keyField.getValue();
		if(key == null || key == ''){
			Ext.Msg.alert('系统信息','用户名不能为空');
			return;
		}
		userStore.load({
			params:{
				key:key,
				search:true
			}
		})
	}
	//更新用户状态
	function updateState(records,state){
		var len = records.length;
		if(len == 0){
			Ext.Msg.alert('系统信息','请先选择用户');
			return false;
		}
		var userIds = new Array();
		Ext.Msg.wait("正在处理……");
		for(var i = 0; i < len; i++){
			userIds.push(records[i].data.userid);
		}
		Ext.Ajax.request({
			url:'/wsd/admin/user_updateState.action',
			params:{
				userIds:userIds,
				state:state
			},
			success:function(response){
				userStore.reload();
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','处理成功');
			},
			failure:function(){
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','处理失败');
			}
		})
	}
	function createUpdateLevelWindow(records){
		var len = records.length;
		if(len == 0){
			Ext.Msg.alert('系统信息','请先选择用户');
			return false;
		}
		var levelCombo = new Ext.form.ComboBox({
			fieldLabel:'会员级别',
			mode:'local',
			name:'text',
			triggerAction:'all',
			valueField:'user.userLevel',
			displayField:'userLevel',
			resizable:false,
			value:1,
			editable:false,
			store:new Ext.data.SimpleStore({
				fields:['user.userLevel','userLevel'],
				data:[
					['1','普通会员'],
					['2','高级会员'],
					['3','VIP会员']
				]
			})
		})
		var modifyBtn = new Ext.Button({
			text:'修改',
			handler:function(){
				var userLevel = levelCombo.getValue();
				updateLevel(records,userLevel);
				levelWindow.close();
			}
		})
		var levelWindow = new Ext.Window({
			layout:'form',
			title:'修改会员级别',
			closeAction:'hide',
			modal:true,
			constrain:true,//防止窗口超出边界,
			items:[levelCombo],
			buttons:[modifyBtn]
		})
		
		levelWindow.show();
	}
	function updateLevel(records,userLevel){
		var userIds = new Array();
		var len = records.length;
		Ext.Msg.wait("正在处理……");
		for(var i = 0; i < len; i++){
			userIds.push(records[i].data.userid);
		}
		Ext.Ajax.request({
			url:'/wsd/admin/user_updateLevel.action',
			params:{
				userLevel:userLevel,
				userIds:userIds
			},
			success:function(response){
				userStore.reload();
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','处理成功');
			},
			failure:function(){
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','处理失败');
			}
			
		})
	}
	
});